package com.easy.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.SysPermission;
import com.easy.common.bean.SysRole;
import com.easy.common.bean.SysUser;
import com.easy.common.service.IUserService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    // 插入用户
    @PostMapping("/")
    public ResultData insert(@RequestBody SysUser sysUser) {
        try {
            SysUser data = userService.insert(sysUser);
            return ResultData.success(data);
        } catch (Exception e) {
            return ResultData.fail("插入用户失败"+e.getMessage());
        }
    }

    // 更新用户
    @PutMapping("/")
    public ResultData update(@RequestBody SysUser sysUser) {
        SysUser data = userService.update(sysUser);
        return ResultData.success(data);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id) {
        int result = userService.delete(id);
        if (result >= 1) {
            return ResultData.success("200", "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    // 根据 ID 获取用户
    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id) {
        SysUser data = userService.getById(id);
        return ResultData.success(data);
    }

    // 分页获取用户列表
    @GetMapping("/page")
    public ResultData getListByPage(Page page, SysUser sysUser) {
        IPage<SysUser> data = userService.getListByPage(page, sysUser);
        return ResultData.success(data);
    }

    // 获取用户列表
    @GetMapping("/list")
    public ResultData getList(SysUser sysUser) {
        List<SysUser> data = userService.getList(sysUser);
        return ResultData.success(data);
    }

    // 设置用户状态
    @PutMapping("/status/{id}")
    public ResultData setStatus(@PathVariable Integer id) {
        int result = userService.setStatus(id);
        if (result >= 1) {
            return ResultData.success("200", "状态设置成功", null);
        } else {
            return ResultData.fail();
        }
    }

    // 为用户分配角色
    @PutMapping("/assignRole")
    public ResultData assignRole(@RequestBody SysUser sysUser) {
        SysUser data = userService.assignRole(sysUser);
        return ResultData.success(data);
    }

    // 为角色分配权限
    @PutMapping("/assignPermission")
    public ResultData assignPermission(@RequestBody SysRole sysRole, @RequestBody SysPermission sysPermission) {
        int result = userService.assignPermission(sysRole, sysPermission);
        if (result >= 1) {
            return ResultData.success("200", "权限分配成功", null);
        } else {
            return ResultData.fail();
        }
    }

    // 根据用户名获取用户
    @GetMapping("/username/{username}")
    public ResultData getByUsername(@PathVariable String username) {
        SysUser data = userService.getByUsername(username);
        return ResultData.success(data);
    }
}